<?php

/**
 * Created by PhpStorm.
 * User: zhengze
 * Date: 2019/7/1
 * Time: 18:29
 */

namespace app\admin\logic;


use app\common\model\MemberGroup;
use app\common\logic\BaseLogic;
use app\common\traits\InstanceTrait;

/**
 * Class MemberGroupLogic
 * @package app\admin\logic
 * @property MemberGroup $model
 */
class MemberGroupLogic extends BaseLogic
{
    use InstanceTrait;

    /**
     * @var MemberGroup
     */
    public $model;

    public function __construct($model = null)
    {
        parent::__construct($model);
    }

    public function getByID($param)
    {
        $MemberGroup = MemberGroup::instance();
        $data = $MemberGroup->getByID($param['id']);
        if (!empty($data)) {
            $data['rule_ids'] = $data->rules->column('rule_id');
            $data['menu_ids'] = $data->menus->column('menu_id');
        }
        return $data;
    }

    /**
     * 获取列表
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     */
    public function getList($param)
    {
        $MemberGroup = MemberGroup::instance();
        $data = $MemberGroup->getList($param);
        $data->load('members')->visible(['members.member_id']);
        return $data;
    }

    public function updateByID($param)
    {
        $data = MemberGroup::instance()->updateByID($param, $param[MemberGroup::instance()->getPk()] ?? $param['id']);
        return $data;
    }


    /**
     * 获取统计
     * @param $param
     * @return mixed|\think\Model|\think\model\Pivot|null
     */
    public function getCount($param)
    {
        $data = MemberGroup::instance()->getCount($param);
        return $data;
    }


    /**
     * 插入数据
     * @param $param
     * @return bool
     */
    public function createData($param)
    {
        $data = MemberGroup::instance()->createData($param);
        return $data;
    }


    /**
     * 删除数据
     * @param $param
     * @return bool
     */
    public function delByID($param)
    {
        $data = MemberGroup::instance()->delByID($param[MemberGroup::instance()->getPk()] ?? $param['id'], false);
        return $data;
    }

    /**
     * 批量删除
     * @param $param
     * @return bool
     */
    public function delByIDs($param)
    {
        $data = MemberGroup::instance()->delByIDs($param[MemberGroup::instance()->getPk().'s'] ??$param['ids'], false);
        return $data;
    }

    /**
     * 启用禁用
     * @param $param
     * @return bool
     */
    public function enableByIDs($param)
    {
        $data = MemberGroup::instance()->enableByIDs($param[MemberGroup::instance()->getPk().'s'] ??$param['ids'], $param['status'], false);
        return $data;
    }
}
